﻿using Bangle.BLL;
using Bangle.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Phone_PhoneTypeList : BasePage
{
    #region 属性
    private string StrWhere
    {
        get
        {
            return ViewState["StrWhere"] == null ? "" : ViewState["StrWhere"].ToString();
        }
        set
        {
            ViewState["StrWhere"] = value;
        }
    }
    #endregion

    T_PhoneTypeBLL bll = new T_PhoneTypeBLL();

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        //检验用户权限
        setUserControlStatus();

        if (!IsPostBack)
        {
            //设置分页控件文本
            setLanguage();

            //绑定数据
            BindRepeaterData("1=1");
        }
    }

    /// <summary>
    /// 设置分页控件文本
    /// </summary>
    private void setLanguage()
    {
        TextUtility.SetPaginateCtlText(PaginateCtl);
    }
    #endregion

    #region 检验用户权限
    private void setUserControlStatus()
    {
        //检验用户权限
        base.CheckUserPermission("002001");
        this.btn_Add.Enabled = base.CheckUserPermissionOperation("002001", "Add");
    }
    #endregion

    #region 数据绑定
    private void BindRepeaterData(string swhere)
    {
        PaginateCtl.PageSize = base.CurrentSession.PageSize;
        int pageIndex = PaginateCtl.CurrentPageIndex;
        int pageSize = PaginateCtl.PageSize;

        UtilityBLL ubll = new UtilityBLL();

        int rCount = 0;
        DataTable dt = ubll.GetListByPage("T_PhoneType", "*", "PhoenTypeName", swhere, pageSize, pageIndex, 0, out rCount);

        //绑定数据源
        RepeaterList.DataSource = dt;
        //返回记录条数
        PaginateCtl.RecordCount = rCount;

        //设置记录总数的显示文本(此方法必须在设置PaginateCtl.RecordCount之后调用)
        TextUtility.SetPaginateCtlRecoText(PaginateCtl);
        RepeaterList.DataBind();
    }
    #endregion

    #region 查询
    /// <summary>
    /// 查询按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        string swhere = "1=1";
        if (this.txt_PhoneTypeName.Text.Trim().Length > 0)
        {
            swhere += " and PhoneTypeName like '%" + TextUtility.ReplaceText(this.txt_PhoneTypeName.Text.Trim()) + "%'";
        }
        StrWhere = swhere;
        //分页控件回到第1页
        PaginateCtl.CurrentPageIndex = 1;
        BindRepeaterData(StrWhere);
    }
    #endregion

    #region 列表操作
    protected void RepeaterList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Label lbl_Valid = (Label)e.Item.FindControl("lbl_Valid");
            LinkButton lbtn_Valid = (LinkButton)e.Item.FindControl("lbtn_Valid");

            lbtn_Valid.Text = "修改";
            //设置启用禁用的显示文本
            if (Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "Valid")) == 0)
            {
                lbl_Valid.Text = "禁用";
                lbl_Valid.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                lbl_Valid.Text = "启用";
                lbl_Valid.ForeColor = System.Drawing.Color.Green;
            }

            #region 用户权限
            LinkButton lbtn_Detail = (LinkButton)e.Item.FindControl("lbtn_Detail");
            LinkButton lbtn_Modify = (LinkButton)e.Item.FindControl("lbtn_Modify");
            LinkButton lbtn_Delete = (LinkButton)e.Item.FindControl("lbtn_Delete");

            lbtn_Detail.Visible = base.CheckUserPermissionOperation("002001", "View");
            lbtn_Modify.Visible = base.CheckUserPermissionOperation("002001", "Edit");
            lbtn_Delete.Visible = base.CheckUserPermissionOperation("002001", "Del");
            lbtn_Valid.Visible = base.CheckUserPermissionOperation("002001", "Valid");
            #endregion
        }
    }
    protected void RepeaterList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string id = e.CommandArgument.ToString();
        if (e.CommandName == "Valid")
        {
            try
            {
                T_PhoneType model = bll.GetModel(new Guid(id));
                model.Valid = model.Valid == 0 ? 1 : 0;
                bll.Update(model);
                BindRepeaterData(StrWhere);
            }
            catch (Exception ex)
            {
                base.Alert(ex.ToString());
            }

        }
        else if (e.CommandName == "Modify")
        {
            Response.Redirect("PhoneTypeEdit.aspx?Action=Modify&id=" + id);
        }
        else if (e.CommandName == "Detail")
        {
            Response.Redirect("PhoneTypeEdit.aspx?Action=Detail&id=" + id);
        }
    }
    #endregion

    #region 分页
    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void PaginateCtl_PageChanged(object sender, EventArgs e)
    {
        BindRepeaterData(StrWhere);
    }
    #endregion
}